package com.piggy.settlement.sdk.client.v2.bankreceipt;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.piggy.settlement.sdk.constant.v2.PiggyBankReceiptConstants;
import com.piggy.settlement.sdk.util.v2.PiggyOpenBasicsVerifyUtil;

/**
 * @author ：daxia
 * @description：开放平台基础能力
 * @modified By：
 * @version: 1.0$
 */
public class PiggyBankReceiptClient {

    static PiggyBankReceiptClient piggyBankReceiptClient = null;

    private String appSecret = new String();
    private String domain = new String();

    public PiggyBankReceiptClient(String appSecret, String domain) {
        this.appSecret = appSecret;
        this.domain = domain;
    }

    public static PiggyBankReceiptClient getInstanse(String appSecret, String domain) {
        if (piggyBankReceiptClient == null) {
            piggyBankReceiptClient =
                    new PiggyBankReceiptClient(appSecret, domain);
        }

        return piggyBankReceiptClient;
    }


    /**
     * 查询单笔交易回单接口
     *
     * @param basics 参数实体：
     * @return
     */
    public JSONObject getSingleReceipt(Object basics) {

        try {
            String signStr = PiggyOpenBasicsVerifyUtil.getSigStr(appSecret, basics);

            JSONObject paramObj = JSONUtil.parseObj(basics);
            paramObj.putOpt("sign", signStr);
            String response = HttpRequest.post(domain + PiggyBankReceiptConstants.GET_SINGLE_RECEIPT)
                    .body(paramObj.toString())
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("V2-查询单笔交易回单失败:");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 查询历史交易回单接口
     *
     * @param basics 参数实体：
     * @return
     */
    public JSONObject getHistoryReceipt(Object basics) {

        try {
            String signStr = PiggyOpenBasicsVerifyUtil.getSigStr(appSecret, basics);

            JSONObject paramObj = JSONUtil.parseObj(basics);
            paramObj.putOpt("sign", signStr);
            String response = HttpRequest.post(domain + PiggyBankReceiptConstants.GET_HISTORY_RECEIPT)
                    .body(paramObj.toString())
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("V2-查询历史交易回单失败:");
            e.printStackTrace();
            return null;
        }

    }

}
